package algorithm.tree;

import algorithm.model.TreeNode;

/**
 * leetcode
 * https://leetcode-cn.com/problems/sum-of-left-leaves/
 * Easy
 *
 * 左叶子值的和
 * Created by yzy on 2021-04-01 16:43
 */
public class SumOfLeftLeaves {

    public static void main(String[] args) {
        TreeNode node = TreeNode.getTreeNode(new Integer[]{3,9,20,null,null,15,7});
        sumOfLeftLeaves(node);
    }

    /**
     * 递归思路，判断左叶子即可
     * 0ms, beats 100.0%
     * 36.2MB, 61.81%
     * @param root
     * @return
     */
    public static int sumOfLeftLeaves(TreeNode root) {
        if(root==null) return 0;
        if(root.left != null && root.left.left==null && root.left.right==null){
            return root.left.val + sumOfLeftLeaves(root.right);
        }
        return sumOfLeftLeaves(root.left) + sumOfLeftLeaves(root.right);
    }
}
